﻿using CommonData.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Extend
{
    public static class ExRandom
    {
        /// <summary>
        /// 生成指定长度的数字随机字符串
        /// </summary>
        /// <param name="length">长度</param>
        /// <returns></returns>
        public static string ExGenerateRandom123(this Random random, int length)
        {
            StringBuilder newRandom = new StringBuilder(62);
            Random rd = new Random();
            for (int i = 0; i < length; i++)
            {
                newRandom.Append(DataList.NumberList[rd.Next(DataList.NumberList.Count)]);
            }
            return newRandom.ToString();
        }
        /// <summary>
        /// 生成指定长度的数字+大，小字母随机字符串
        /// </summary>
        /// <param name="length">长度</param>
        /// <returns>字符串</returns>
        public static string ExGenerateRandom(this Random random, int length)
        {
            StringBuilder newRandom = new StringBuilder(62);
            Random rd = new Random();
            for (int i = 0; i < length; i++)
            {
                newRandom.Append(DataList.NumAndGrapList[rd.Next(DataList.NumAndGrapList.Count)]);
            }
            return newRandom.ToString();
        }
        /// <summary>
        /// 生成指定长度的随机字母字符串
        /// </summary>
        /// <param name="length">长度</param>
        /// <returns></returns>
        public static string ExGenerateRandomABC(this Random random, int length)
        {
            StringBuilder newRandom = new StringBuilder(52);
            Random rd = new Random();
            for (int i = 0; i < length; i++)
            {
                newRandom.Append(DataList.GraphemeList[rd.Next(DataList.GraphemeList.Count)]);
            }
            return newRandom.ToString();
        }
        /// <summary>
        /// 根据当前日期生成随机数，至少16位
        /// </summary>
        /// <param name="str">前缀</param>
        /// <returns></returns>
        public static string ExGenerateRandomByDateTimeNow(this Random random, string str = "")
        {
            return str + DateTime.Now.ToString("yyyyMMddHHmmssff");
        }
        /// <summary>
        /// 生成随机流水号
        /// </summary>
        /// <param name="random">Random</param>
        /// <param name="length">流水号总长度</param>
        /// <param name="str">不够总长度时前面补的字符</param>
        /// <returns>随机流水号</returns>
        public static string ExGenerateSerialNumber(this Random random, int length, char str = '0')
        {
            DateTime dt = new DateTime(2015, 8, 1);
            return Math.Floor(dt.ExDiff(DateTime.Now, TimeSpanType.s)).ToString().PadLeft(length, str);
        }
    }
}
